@echo off
setlocal enabledelayedexpansion

set BINARY_NAME=blog
set BUILD_DIR=bin
set CONFIG_FILE=config/config.yaml

if "%1"=="" goto help
if "%1"=="help" goto help
if "%1"=="config" goto config
if "%1"=="build" goto build
if "%1"=="run" goto run
if "%1"=="run-prod" goto run-prod
if "%1"=="dev" goto dev
if "%1"=="test" goto test
if "%1"=="clean" goto clean
if "%1"=="fmt" goto fmt
if "%1"=="config-prod" goto config-prod
goto help

:config
echo 生成默认配置文件...
go run cmd/init/main.go -output %CONFIG_FILE%
goto end

:build
echo 安装依赖...
go mod tidy
go mod download
echo 构建应用...
if not exist %BUILD_DIR% mkdir %BUILD_DIR%
go build -o %BUILD_DIR%\%BINARY_NAME%.exe cmd/http/main.go
goto end

:run
echo 安装依赖...
go mod tidy
go mod download
echo 启动应用...
go run cmd/http/main.go
goto end

:run-prod
echo 安装依赖...
go mod tidy
go mod download
echo 启动应用（生产配置）...
go run cmd/http/main.go -config config.prod.yaml
goto end

:dev
echo 开发模式启动（需要先安装 gowatch: go install github.com/silenceper/gowatch@latest）...
if not exist %BUILD_DIR% mkdir %BUILD_DIR%
gowatch -o %BUILD_DIR%\%BINARY_NAME%.exe cmd/http/main.go
goto end

:test
echo 运行测试...
go test ./...
goto end

:clean
echo 清理构建文件...
if exist %BUILD_DIR% rmdir /s /q %BUILD_DIR%
go clean
goto end

:fmt
echo 格式化代码...
go fmt ./...
goto end

:config-prod
echo 创建生产环境配置文件...
copy config/config.prod.yaml.example config/config.prod.yaml
echo 请编辑 config/config.prod.yaml 文件，配置生产环境参数
goto end

:help
echo GoBlog 构建脚本使用说明:
echo.
echo 常用命令:
echo   build config      - 生成默认配置文件
echo   build build       - 构建应用
echo   build run         - 运行应用
echo   build run-prod    - 使用生产配置运行应用
echo   build dev         - 开发模式运行（热重载）
echo   build test        - 运行测试
echo   build clean       - 清理构建文件
echo   build fmt         - 格式化代码
echo   build help        - 显示此帮助信息
echo.
echo 配置文件:
echo   build config-prod - 创建生产环境配置文件
echo.
echo 示例:
echo   build config ^&^& build run
goto end

:end
endlocal 